<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Class sub_range</title>
<link rel="stylesheet" href="../../../../../../../doc/html/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../../../index.html" title="Chapter&#160;1.&#160;Range 2.0">
<link rel="up" href="../utilities.html" title="Utilities">
<link rel="prev" href="iterator_range.html" title="Class iterator_range">
<link rel="next" href="join.html" title="Function join">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="iterator_range.html"><img src="../../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../utilities.html"><img src="../../../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="join.html"><img src="../../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
<div class="section" title="Class sub_range">
<div class="titlepage"><div><div><h4 class="title">
<a name="range.reference.utilities.sub_range"></a><a class="link" href="sub_range.html" title="Class sub_range"> Class <code class="computeroutput"><span class="identifier">sub_range</span></code></a>
</h4></div></div></div>
<p>
          The <code class="computeroutput"><span class="identifier">sub_range</span></code> class inherits
          all its functionality from the <a class="link" href="iterator_range.html" title="Class iterator_range"><code class="computeroutput"><span class="identifier">iterator_range</span></code></a> class. The <code class="computeroutput"><span class="identifier">sub_range</span></code> class is often easier to use
          because one must specify the <a class="link" href="../../concepts/forward_range.html" title="Forward Range">Forward
          Range</a> template argument instead of an iterator. Moreover, the <code class="computeroutput"><span class="identifier">sub_range</span></code> class can propagate constness
          since it knows what a corresponding <code class="computeroutput"><span class="identifier">const_iterator</span></code>
          is.
        </p>
<a name="range.reference.utilities.sub_range.synopsis"></a><h5>
<a name="id664954"></a>
          <a class="link" href="sub_range.html#range.reference.utilities.sub_range.synopsis">Synopsis</a>
        </h5>
<p>

</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span>
<span class="special">{</span>
    <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">ForwardRange</span> <span class="special">&gt;</span>
    <span class="keyword">class</span> <span class="identifier">sub_range</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">iterator_range</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">ForwardRange</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="special">&gt;</span>
    <span class="special">{</span>
    <span class="keyword">public</span><span class="special">:</span>
        <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">ForwardRange</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">iterator</span><span class="special">;</span>
        <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">ForwardRange</span><span class="special">&gt;::</span><span class="identifier">type</span>  <span class="identifier">const_iterator</span><span class="special">;</span>
        <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">iterator_difference</span><span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">difference_type</span><span class="special">;</span>

    <span class="keyword">public</span><span class="special">:</span> <span class="comment">// construction, assignment
</span>        <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">ForwardTraversalIterator</span> <span class="special">&gt;</span>
        <span class="identifier">sub_range</span><span class="special">(</span> <span class="identifier">ForwardTraversalIterator</span> <span class="identifier">Begin</span><span class="special">,</span> <span class="identifier">ForwardTraversalIterator</span> <span class="identifier">End</span> <span class="special">);</span>

        <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">ForwardRange2</span> <span class="special">&gt;</span>
        <span class="identifier">sub_range</span><span class="special">(</span> <span class="identifier">ForwardRange2</span><span class="special">&amp;</span> <span class="identifier">r</span> <span class="special">);</span>

        <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">ForwardRange2</span> <span class="special">&gt;</span>
        <span class="identifier">sub_range</span><span class="special">(</span> <span class="keyword">const</span> <span class="identifier">Range2</span><span class="special">&amp;</span> <span class="identifier">r</span> <span class="special">);</span>

        <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">ForwardRange2</span> <span class="special">&gt;</span>
        <span class="identifier">sub_range</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="identifier">ForwardRange2</span><span class="special">&amp;</span> <span class="identifier">r</span> <span class="special">);</span>

        <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">ForwardRange2</span> <span class="special">&gt;</span>
        <span class="identifier">sub_range</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span> <span class="keyword">const</span> <span class="identifier">ForwardRange2</span><span class="special">&amp;</span> <span class="identifier">r</span> <span class="special">);</span>

    <span class="keyword">public</span><span class="special">:</span>  <span class="comment">// Forward Range functions
</span>        <span class="identifier">iterator</span>        <span class="identifier">begin</span><span class="special">();</span>
        <span class="identifier">const_iterator</span>  <span class="identifier">begin</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
        <span class="identifier">iterator</span>        <span class="identifier">end</span><span class="special">();</span>
        <span class="identifier">const_iterator</span>  <span class="identifier">end</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="keyword">public</span><span class="special">:</span> <span class="comment">// convenience
</span>        <span class="identifier">value_type</span><span class="special">&amp;</span>       <span class="identifier">front</span><span class="special">();</span>
        <span class="keyword">const</span> <span class="identifier">value_type</span><span class="special">&amp;</span> <span class="identifier">front</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
        <span class="identifier">value_type</span><span class="special">&amp;</span>       <span class="identifier">back</span><span class="special">();</span>
        <span class="keyword">const</span> <span class="identifier">value_type</span><span class="special">&amp;</span> <span class="identifier">back</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>
        <span class="comment">// for Random Access Range only:
</span>        <span class="identifier">value_type</span><span class="special">&amp;</span>       <span class="keyword">operator</span><span class="special">[](</span> <span class="identifier">size_type</span> <span class="identifier">at</span> <span class="special">);</span>
        <span class="keyword">const</span> <span class="identifier">value_type</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">[](</span> <span class="identifier">size_type</span> <span class="identifier">at</span> <span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="keyword">public</span><span class="special">:</span>
        <span class="comment">// rest of interface inherited from iterator_range
</span>    <span class="special">};</span>

<span class="special">}</span> <span class="comment">// namespace 'boost'
</span></pre>
<p>
        </p>
<p>
          The class should be trivial to use as seen below. Imagine that we have
          an algorithm that searches for a sub-string in a string. The result is
          an iterator_range, that delimits the match. We need to store the result
          from this algorithm. Here is an example of how we can do it with and without
          <code class="computeroutput"><span class="identifier">sub_range</span></code>
        </p>
<p>

</p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">str</span><span class="special">(</span><span class="string">"hello"</span><span class="special">);</span>
<span class="identifier">iterator_range</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">iterator</span><span class="special">&gt;</span> <span class="identifier">ir</span> <span class="special">=</span> <span class="identifier">find_first</span><span class="special">(</span> <span class="identifier">str</span><span class="special">,</span> <span class="string">"ll"</span> <span class="special">);</span>
<span class="identifier">sub_range</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span>               <span class="identifier">sub</span> <span class="special">=</span> <span class="identifier">find_first</span><span class="special">(</span> <span class="identifier">str</span><span class="special">,</span> <span class="string">"ll"</span> <span class="special">);</span>
</pre>
<p>
        </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2003 -2010 Thorsten Ottosen, Neil Groves<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="iterator_range.html"><img src="../../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../utilities.html"><img src="../../../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="join.html"><img src="../../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>
